﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GdHotel.Data;
using GdHotel.Data.Interfaces;
using UniqueTechnologies.Attributes;

namespace GdHotel.Web.Controllers
{
    [LanguageSupport]
    [Authorize(Roles = "Admin")]
    public class ManageRoomsController : BaseController
    {
        public IRoomsRepository RoomsRepository { get; set; }
        public override ILanguagesRepository LanguagesRepository { get; set; }
        public IRoomTypesRepository RoomTypesRepository { get; set; }

        public ManageRoomsController(IRoomsRepository roomsRepository, ILanguagesRepository languagesRepository, IRoomTypesRepository roomTypesRepository)
        {
            RoomsRepository = roomsRepository;
            LanguagesRepository = languagesRepository;
            RoomTypesRepository = roomTypesRepository;
        }

        //
        // GET: /Rooms/

        public ViewResult Index()
        {
            var rooms = RoomsRepository.FindAllByLang(Language);
            return View(rooms);
        }

        //
        // GET: /Rooms/Details/5

        public ViewResult Details(Int32 id)
        {
            var room = RoomsRepository.GetById(id);
            return View(room);
        }

        //
        // GET: /Rooms/Create

        public ActionResult Create()
        {
            ViewBag.RoomTypeId = new SelectList(RoomTypesRepository.FindAllByLang(Language), "Id", "Title");
            return View();
        }

        //
        // POST: /Rooms/Create

        [HttpPost]
        public ActionResult Create(Room room)
        {
            if (ModelState.IsValid)
            {
                room.LanguageId = Language.Id;
                RoomsRepository.Insert(room);
                RoomsRepository.Save();
                return RedirectToAction("Index");
            }
            ViewBag.RoomTypeId = new SelectList(RoomTypesRepository.FindAllByLang(Language), "Id", "Title", room.RoomTypeId);
            return View(room);
        }

        //
        // GET: /Rooms/Edit/5

        public ActionResult Edit(int id)
        {
            var room = RoomsRepository.GetById(id);
            ViewBag.RoomTypeId = new SelectList(RoomTypesRepository.FindAllByLang(Language), "Id", "Title", room.RoomTypeId);
            return View(room);
        }

        //
        // POST: /Rooms/Edit/5

        [HttpPost]
        public ActionResult Edit(Room room)
        {
            var dbRoom = RoomsRepository.GetById(room.Id);
            if (ModelState.IsValid)
            {
                UpdateModel(dbRoom);
                RoomsRepository.Save();
                return RedirectToAction("Index");
            }
            ViewBag.RoomTypeId = new SelectList(RoomTypesRepository.FindAllByLang(Language), "Id", "Title", room.RoomTypeId);
            return View(room);
        }

        //
        // GET: /Rooms/Delete/5

        public ActionResult Delete(int id)
        {
            var room = RoomsRepository.GetById(id);
            return View(room);
        }

        //
        // POST: /Rooms/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            var room = RoomsRepository.GetById(id);
            RoomsRepository.Delete(room);
            RoomsRepository.Save();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            RoomsRepository.Dispose();
            base.Dispose(disposing);
        }
    }
}